<?php
/**
 * 管理中心· 系统设置模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com;
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Controller;
use Think\AjaxPage;
use Hoteladmin\Model\SetupModel;
class SetupController extends BaseController {
    //初始化程序
    public function _initialize()
    {
        parent::_initialize();
				$usident = M('user');
				$type = I('get.type','sDire');
				$conf = tpCache($type);
//			$director5=$usident->where("`us_director5`=1")->select();
//			$d5count=$conf['director5']/count($director5);
//			$director4=$usident->where("`us_director4`=1 and `us_director5`=0 ")->select();
//			$d4count=$conf['director4']/count($director4);
//			$director3=$usident->where("`us_director3`=1 and `us_director4`=0 and `us_director5`=0 ")->select();
//			$d3count=$conf['director3']/count($director3);
//			$director2=$usident->where("`us_director2`=1 and `us_director3`=0 and `us_director4`=0 and `us_director5`=0 ")->select();
//			$d2count=$conf['director2']/count($director2);
//			$director1=$usident->where("`us_director1`=1 and `us_director2`=0 and `us_director3`=0 and `us_director4`=0 and `us_director5`=0 ")->select();
//			$d1count=$conf['director1']/count($director1);
//			$this->assign('d5count',$d5count);
//			$this->assign('d4count',$d4count);
//				$this->assign('d3count',$d3count);
//				$this->assign('d2count',$d2count);
//				$this->assign('d1count',$d1count);
        $this->notice_type= C('NOTICE_TYPE');
        $this->user_state = C('USER_STATE');
        $this->assign('ntype',$this->notice_type); //公告类型
        $timegap['beg'] = date('Y-m-d',strtotime("-1 year"));//1年前
        $timegap['end'] = date('Y-m-d',strtotime("+1 days"));
        $this->assign('timegap',$timegap);
    }
	/**
	 +--------------------------------------------------------------------------
	 * 实现系统参数的配置入口
	 * @author:  XC跃
	 * @date :2018-03-14
	 +--------------------------------------------------------------------------
	 */
	public function shifang(){
		$data = I('post.');
		$type = $data['type'];
	  do_zuoYou();
		do_yeji();
		$res = array('status'=>1,'msg'=>'奖励释放成功','url'=>U('Setup/index?type='.$type));
		exit(json_encode($res));
	}
	public function index(){
	    $grident = M('grade');
        $arr = $grident->where(array("gr_id"=>array("in", array(7,8,9,10))))->select();

    	/*配置列表*/
		$set_nav = array(
		    'sBas'=>'基本信息','sIno'=>'交易设置','sMes'=>'短信设置'/*,'sDire'=>'董事设置'*/
		);		
		//dump($set_nav);
		$type = I('get.type','sIno');
//		$conf = tpCache($type);
		$setup = M('setup')->where(array('se_type'=>$type))->select();
        $conf = array_column($setup, 'se_value','se_name');
		$this->assign('set_nav',$set_nav);
		$this->assign('type',$type);
		$this->assign('conf',$conf); //当前配置项
		$this->assign('arr',$arr); //等级表
        $this->display($type);
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现新增修改系统参数的功能
	 * @author:  XC跃
	 * @date :2018-03-14
	 +--------------------------------------------------------------------------
	 */
	public function setupUpdate(){
		//接收传值
		$data = I('post.');
		$grident = M('grade');
        $array_ab=array_combine($data['gid'],$data['gra']);
        foreach ($array_ab as $k =>$v){
            $grident->where(array('gr_id'=>$k))->save(array('gr_money'=>$v));
        }
        unset($data['gra']);
        unset($data['gid']);
		$type = $data['type'];
		unset($data['type']);
		tpCache($type,$data);

		$res = array('status'=>1,'msg'=>'系统参数更新成功','url'=>U('Setup/index?type='.$type));
		exit(json_encode($res));
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现获取系统操作日志的功能
	 * @author:  XC跃
	 * @date :2018-05-04
	 +--------------------------------------------------------------------------
	 */
	public function logList(){
	    $this->display();
	}
	public function ajaxLogList(){
	    //实例化相关表
	    $loident = M('logs');
	    // 搜索条件
	    $beg = strtotime(I('stime'));
	    $end = strtotime(I('etime'));
	    $condition = array();
	    I('style') ? $condition['lo_style'] = I('style') :false;
	    I('uname') ? $condition['lo_uid'] = array('LIKE',"%".I('uname')."%") : false;
	    I('type') ? $condition['lo_type'] = I('type') :false;
	    if($beg && $end){
	        $condition['lo_time'] = array('between',"$beg,$end");
	    }
	    $sort_order = I('order_by').' '.I('sort');
	    $count = $loident->where($condition)->count();
	    $Page  = new AjaxPage($count,50);
	    $show = $Page->show();
	    $list = $loident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
	    //echo $loident->getLastSql();
	    $this->assign('list',$list);
	    $this->assign('page',$show);// 赋值分页输出
	    $this->assign('rows',$Page->totalRows);
	    $this->display();
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现删除系统日志的功能
	 * @author:XC跃
	 * @date :2018-05-06
	 +--------------------------------------------------------------------------
	 */
	public function logDel(){
	    $id = I('id/d');
	    //实例化相关表
	    $ident = M('logs');
	    $re = $ident->where("`lo_id`='".$id."'")->delete();
	    //日志
	    addLog(session('admin_auth.usname'),'2','2',"删除日志数据ID".$id."");
	    if($re>0){
	        $res = array('status'=>1,'info'=>'删除成功');
	        exit(json_encode($res));
	    }
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现批量删除系统日志的功能
	 * @author:ZJ
	 * @date :2018-10-12
	 +--------------------------------------------------------------------------
	 */
	public function dataDel(){
	    $ids = I('post.ids');
	    $ids=substr($ids,0,strlen($ids)-1);
	    $ids = explode(",",$ids);
	    $ids = "'" . join("','", $ids) . "'";
	    //实例化相关表
	    $ident = M('logs');
	    $re = $ident->where("`lo_id` in ($ids)")->delete();
	    //echo $ident->getLastSql();
	    //日志
	    addLog(session('admin_auth.usname'),'2','2',"删除日志数据ID".$ids."");
	    if($re>0){
	        $res = array('status'=>1,'info'=>'删除成功');
	        exit(json_encode($res));
	    }
	}
	
	/**
	 +--------------------------------------------------------------------------
	 * 实现清空数据库数据
	 * @author:  XC跃
	 * @date :2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function doClear(){
	    if(IS_POST){
	        $pwd1 = I('post.pwd');
	        //设定密码： www.unitewww.com
	        $pwd2 = md5('www.unitewww.com');
	        if(md5($pwd1) != $pwd2){
	            $res = array('status'=>-1,'msg'=>'密保安全码不正确');
	            exit(json_encode($res));
	        }
	        //echo md5($pwd1)."<br/>".$pwd2;die;
	        //清空表
	        M()->execute('TRUNCATE TABLE `xc_account`');
	        M()->execute('TRUNCATE TABLE `xc_address`');
	        M()->execute('TRUNCATE TABLE `xc_bank`');
	        M()->execute('TRUNCATE TABLE `xc_cart`');
	        M()->execute('TRUNCATE TABLE `xc_code`');
	        M()->execute('TRUNCATE TABLE `xc_collect`');
	        M()->execute('TRUNCATE TABLE `xc_coupon`');
	        M()->execute('TRUNCATE TABLE `xc_coupon_list`');
	        M()->execute('TRUNCATE TABLE `xc_logs`');
	        M()->execute('TRUNCATE TABLE `xc_history`');
	        M()->execute('TRUNCATE TABLE `xc_message`');
	        M()->execute('TRUNCATE TABLE `xc_notice`');
	        M()->execute('TRUNCATE TABLE `xc_order`');
	        M()->execute('TRUNCATE TABLE `xc_order_details`');
	        M()->execute('TRUNCATE TABLE `xc_order_remarks`');
	        M()->execute('TRUNCATE TABLE `xc_prop_order`');
	        M()->execute('TRUNCATE TABLE `xc_sms`');
	        M()->execute('TRUNCATE TABLE `xc_user`');
	        //插入初始数据
	        M()->execute("INSERT INTO `xc_user` (`us_uid`,`us_pid`, `us_name`, `us_pwd`, `us_pwds`, `us_phone`, `us_atime`,`us_state`) VALUES
		        ('88888888','0', '尚善福', '".md5('123456')."','".md5('123456')."','18588888888','".time()."', '3')");
	        $res = array('status'=>1,'msg'=>'数据清空成功','url'=>U('Index/welcome'));
	        exit(json_encode($res));
	    }else{
	        $this->display();
	    }
	}
	/**
	 +--------------------------------------------------------------------------
	 * 获取运费模板列表
	 * @author:  ZJ
	 * @date :2018-09-10
	 +--------------------------------------------------------------------------
	 */
	public function freightList(){
	    $this->display();
	}
	public function ajaxFreightList(){
	    //实例化相关表
	    $frident = M('freight');
	    $reident = M('region');
	    // 搜索条件
	    $condition = array();
	    I('pname') ? $condition['fr_region'] = array('LIKE',"%".I('pname')."%") : false;
	    //dump($condition);
	    $count = $frident->where($condition)->count();
	    $Page  = new AjaxPage($count,50);
	    $show = $Page->show();
	    $list = $frident->where($condition)->order('fr_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    // echo $frident->getLastSql();
	    foreach($list as $key=>$val){
	        if('默认' != $val['fr_region']){
	            $val = json_decode($val['fr_region']);
	            $region = "'" . join("','", $val) . "'";
	            $regionList = $reident->where("`re_id` in ($region)")->field('re_name')->select();
	            $regionList = array_column($regionList,'re_name');
	            $regionList = implode(',',$regionList);
	            $list[$key]['fr_region'] = $regionList;
	        }
	    }
	    $this->assign('list',$list);
	    $this->assign('page',$show);// 赋值分页输出
	    $this->assign('rows',$Page->totalRows);
	    $this->display();
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现运费模板的删除功能
	 * @author : ZJ
	 * @date：2018-09-10
	 +--------------------------------------------------------------------------
	 */
	public function delFreight(){
	    //接收相关值
	    $id = I('post.id/d');
	    //实例化相关表
	    $frident = M('freight');
	    $re = $frident->where("`fr_id`='".$id."'")->delete();
	    if($re>0){
	        $res = array('status'=>1,'msg'=>'删除成功');
	        exit(json_encode($res));
	    }
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现添加运费模板的功能
	 * @author:  XC跃
	 * @date :2018-09-18
	 +--------------------------------------------------------------------------
	 */
	public function freightAddEdit(){
	    $id = I('id/d');
	    //实例化相关表
	    $frident = M('freight');
	    $reident = M('region');
	    //实例化相关Model
	    $uidentr = new SetupModel();
	    if(IS_POST){
	        $fid = I("post.fid/d");
	        $re = $uidentr->freight_add_edit($fid);
	        if('1'==$re){
	            $res = array('status'=>1,'msg'=>'保存成功');
	        }else{
	            $res = array('status'=>-1,'msg'=>'已有默认模板');
	        }
	        exit(json_encode($res));
	    }else{
	        $detail = array();
	        if($id){
	            $ure = $frident->where("`fr_id`='".$id."'")->find();
	            $detail['rid'] = json_decode($ure['fr_region']);
	            $this->assign('ure',$ure);
	            $this->assign('act','edit');
	        }
	        $freight = $frident->field('fr_region')->select();
	        foreach($freight as $vals){
	            $regionArr = json_decode($vals['fr_region']);
	            //dump($vals['fr_region']);
	            if(is_array($regionArr)){
	                $region[] = implode(',',$regionArr);
	            }
	        }
	        //把数组中的多个字符串合并成一个数组
	        $region = implode(',',$region);
	        $region = explode(',',$region);
	        //获取全部地区
	        $regList = $this->getRegTree($region,$detail);
	        	
	        $this->assign('regList',$regList);
	        $this->display();
	    }
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现地区之间的树状结构
	 * @author:  XC跃
	 * @date :2018-09-18
	 +--------------------------------------------------------------------------
	 */
	public function getRegTree($region,$detail){
	    //实例化相关表
	    $reident = M('region');
	    $regList = $reident->where("`re_parent_id`='0' and `re_level`=1")->order('re_id')->select();
	    //dump($regList);
	    foreach ($regList as $key=>$val){
	        $regList[$key]['disabled'] = in_array($val['re_id'],$region)?'1':'0';
	        if(!empty($detail)){
	            $regList[$key]['enable'] = in_array($val['re_id'],$detail['rid'])?'1':'0';
	        }
	        $regChild = $reident->where("`re_parent_id`='".$val['re_id']."' and `re_level`=2")->order('re_id')->select();
	        foreach ($regChild as $ckey=>$cval){
	            $regList[$key]['child'][$ckey] = $cval;
	            $regList[$key]['child'][$ckey]['disabled'] = in_array($cval['re_id'],$region)?'1':'0';
	            if(!empty($detail)){
	                $regList[$key]['child'][$ckey]['enable'] = in_array($cval['re_id'],$detail['rid'])?'1':'0';
	            }
	        }
	    }
	    return $regList;
	}


	/******************************* 公告列表  *********************************************/
	/**
	+--------------------------------------------------------------------------
	 * 获取公告列表
	 * @author:  XC跃
	 * @date :2018-03-14
	+--------------------------------------------------------------------------
	 */
	public function noticeList(){
		$this->display();
	}
	public function ajaxNoticeList(){
		//实例化相关表
		$noident = M('notice');
		// 搜索条件
		$beg = strtotime(I('stime'));
		$end = strtotime(I('etime'));
		$condition = array();
		I('type') ? $condition['no_cid'] = I('type') : false;
		I('nname') ? $condition['no_title'] = array('LIKE',"%".I('nname')."%") : false;
		if($beg && $end){
			$condition['no_atime'] = array('between',"$beg,$end");
		}
		//dump($condition);
		$sort_order = I('order_by').' '.I('sort');
		//echo $sort_order;
		$count = $noident->where($condition)->count();
		$Page  = new AjaxPage($count,50);
		$show = $Page->show();
		$list = $noident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
		//echo $usident->getLastSql();
		$this->assign('list',$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('rows',$Page->totalRows);
		$this->display();
	}
	/**
	+--------------------------------------------------------------------------
	 * 实现公告列表的添加(修改)功能
	 * @author : XC跃
	 * @date：2018-03-14
	+--------------------------------------------------------------------------
	 */
	public function notiAddEdit(){
		//接收传值
		$nid = I('id');
		//实例化相关Model
		$sidnetr = New SetupModel();
		if(IS_POST){
			$re = $sidnetr->notiAddEdit($nid);
			if('1'==$re){
				$res = array('status'=>1,'msg'=>'保存成功');
				exit(json_encode($res));
			}
		}else{
			if(!empty($nid)){
				$res = M('notice')->where("`no_id`='".$nid."'")->find();
				$this->assign('res',$res);
			}
			$this->display();
		}
	}
	/**
	+--------------------------------------------------------------------------
	 * 实现系统进出账比例显示功能
	 * @author :GH
	 * @date：2019-09-03
	+--------------------------------------------------------------------------
	 */
	public function money(){
		$this->display();
	}
		public function ajaxMoneyList(){
		//实例化相关Model
		$moident = M('money');
		$beg = strtotime(I('stime'));
		$end = strtotime(I('etime'));
		$condition = array();
		I('type') ? $condition['sm_id'] = I('type') : false;
		if($beg && $end){
			$condition['mo_atime'] = array('between',"$beg,$end");
		}
		$sort_order = I('order_by').' '.I('sort');
		//echo $sort_order;
		$count = $moident->where($condition)->count();
		$Page  = new AjaxPage($count,50);
		$show = $Page->show();
		$list = $moident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
		//echo $usident->getLastSql();
//		dump($list);
		$this->assign('list',$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('rows',$Page->totalRows);
		$this->display();
	}
}